package com.future.hedongjun.home.utils;

import android.util.Base64;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by 宫智耀 on 2016/10/13.
 */
public class DES { //初始化向量，随意填写
    // 密钥
    private final static String secretKey = "greatkno";
    private static byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0};

    public static String decode(String encryptText){
        try {
            SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "DES");
            Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decryptData = cipher.doFinal(Base64.decode(encryptText, 1));
            return new String(decryptData).trim();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return "";
    }

    public static String encode(String plainText) {
        try {
            SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "DES");
            Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] encryptedData = cipher.doFinal(plainText.getBytes());
            return Base64.encodeToString(encryptedData, 1);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return "";
    }

}
